﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using WMSADBUtility;
using WMSAML.Warehouse;

namespace WMSADL.Warehouse
{
    public class PD_PostionDAL
    {
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int NID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from PD_Position where NID=@NID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NID", DbType.Int32, NID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(PD_PostionEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into PD_Position(");
            strSql.Append("WarehouseNO,AreaNO,PositionNO,PositionName,Stauts,Designated_Stauts,CapacityUpper,CapacityLower,WeightLimit,CreatTime,Mark,field1,field2,field3,field4,field5)");

            strSql.Append(" values (");
            strSql.Append("@WarehouseNO,@AreaNO,@PositionNO,@PositionName,@Stauts,@Designated_Stauts,@CapacityUpper,@CapacityLower,@WeightLimit,@CreatTime,@Mark,@field1,@field2,@field3,@field4,@field5)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "WarehouseNO", DbType.String, entity.WarehouseNO);
            db.AddInParameter(dbCommand, "AreaNO", DbType.String, entity.AreaNO);
            db.AddInParameter(dbCommand, "PositionNO", DbType.String, entity.PositionNO);
            db.AddInParameter(dbCommand, "PositionName", DbType.String, entity.PositionName);
            db.AddInParameter(dbCommand, "Stauts", DbType.String, entity.Stauts);
            db.AddInParameter(dbCommand, "Designated_Stauts", DbType.String, entity.Designated_Stauts);
            db.AddInParameter(dbCommand, "CapacityUpper", DbType.Decimal, entity.CapacityUpper);
            db.AddInParameter(dbCommand, "CapacityLower", DbType.Decimal, entity.CapacityLower);
            db.AddInParameter(dbCommand, "WeightLimit", DbType.Decimal, entity.WeightLimit);
            db.AddInParameter(dbCommand, "CreatTime", DbType.DateTime, entity.CreatTime);
            db.AddInParameter(dbCommand, "Mark", DbType.String, entity.Mark);
            db.AddInParameter(dbCommand, "field1", DbType.String, entity.field1);
            db.AddInParameter(dbCommand, "field2", DbType.String, entity.field2);
            db.AddInParameter(dbCommand, "field3", DbType.String, entity.field3);
            db.AddInParameter(dbCommand, "field4", DbType.String, entity.field4);
            db.AddInParameter(dbCommand, "field5", DbType.String, entity.field5);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PD_PostionEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PD_Position set ");
            strSql.Append("WarehouseNO=@WarehouseNO,");
            strSql.Append("AreaNO=@AreaNO,");
            strSql.Append("PositionNO=@PositionNO,");
            strSql.Append("PositionName=@PositionName,");
            strSql.Append("Stauts=@Stauts,");
            strSql.Append("Designated_Stauts=@Designated_Stauts,");
            strSql.Append("CapacityUpper=@CapacityUpper,");
            strSql.Append("CapacityLower=@CapacityLower,");
            strSql.Append("WeightLimit=@WeightLimit,");
            strSql.Append("CreatTime=@CreatTime,");
            strSql.Append("Mark=@Mark,");
            strSql.Append("field1=@field1,");
            strSql.Append("field2=@field2,");
            strSql.Append("field3=@field3,");
            strSql.Append("field4=@field4,");
            strSql.Append("field5=@field5");
            strSql.Append(" where NID=@NID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NID", DbType.Int32, entity.NID);
            db.AddInParameter(dbCommand, "WarehouseNO", DbType.String, entity.WarehouseNO);
            db.AddInParameter(dbCommand, "AreaNO", DbType.String, entity.AreaNO);
            db.AddInParameter(dbCommand, "PositionNO", DbType.String, entity.PositionNO);
            db.AddInParameter(dbCommand, "PositionName", DbType.String, entity.PositionName);
            db.AddInParameter(dbCommand, "Stauts", DbType.String, entity.Stauts);
            db.AddInParameter(dbCommand, "Designated_Stauts", DbType.String, entity.Designated_Stauts);
            db.AddInParameter(dbCommand, "CapacityUpper", DbType.Decimal, entity.CapacityUpper);
            db.AddInParameter(dbCommand, "CapacityLower", DbType.Decimal, entity.CapacityLower);
            db.AddInParameter(dbCommand, "WeightLimit", DbType.Decimal, entity.WeightLimit);
            db.AddInParameter(dbCommand, "CreatTime", DbType.DateTime, entity.CreatTime);
            db.AddInParameter(dbCommand, "Mark", DbType.String, entity.Mark);
            db.AddInParameter(dbCommand, "field1", DbType.String, entity.field1);
            db.AddInParameter(dbCommand, "field2", DbType.String, entity.field2);
            db.AddInParameter(dbCommand, "field3", DbType.String, entity.field3);
            db.AddInParameter(dbCommand, "field4", DbType.String, entity.field4);
            db.AddInParameter(dbCommand, "field5", DbType.String, entity.field5);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PD_PostionEntity GetEntity(int NID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,WarehouseNO,AreaNO,PositionNO,PositionName,Stauts,Designated_Stauts,CapacityUpper,CapacityLower,WeightLimit,CreatTime,Mark,field1,field2,field3,field4,field5 from PD_Position ");
            strSql.Append(" where NID=@NID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NID", DbType.Int32, NID);
            PD_PostionEntity entity = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    entity = ReaderBind(dataReader);
                }
            }
            return entity;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PD_PostionEntity DataRowToentity(DataRow row)
        {
            PD_PostionEntity entity = new PD_PostionEntity();
            if (row != null)
            {
                if (row["NID"] != null && row["NID"].ToString() != "")
                {
                    entity.NID = int.Parse(row["NID"].ToString());
                }
                if (row["WarehouseNO"] != null)
                {
                    entity.WarehouseNO = row["WarehouseNO"].ToString();
                }
                if (row["AreaNO"] != null)
                {
                    entity.AreaNO = row["AreaNO"].ToString();
                }
                if (row["PositionNO"] != null)
                {
                    entity.PositionNO = row["PositionNO"].ToString();
                }
                if (row["PositionName"] != null)
                {
                    entity.PositionName = row["PositionName"].ToString();
                }
                if (row["Stauts"] != null)
                {
                    entity.Stauts = row["Stauts"].ToString();
                }
                if (row["Designated_Stauts"] != null)
                {
                    entity.Designated_Stauts = row["Designated_Stauts"].ToString();
                }
                if (row["CapacityUpper"] != null && row["CapacityUpper"].ToString() != "")
                {
                    entity.CapacityUpper = decimal.Parse(row["CapacityUpper"].ToString());
                }
                if (row["CapacityLower"] != null && row["CapacityLower"].ToString() != "")
                {
                    entity.CapacityLower = decimal.Parse(row["CapacityLower"].ToString());
                }
                if (row["WeightLimit"] != null && row["WeightLimit"].ToString() != "")
                {
                    entity.WeightLimit = decimal.Parse(row["WeightLimit"].ToString());
                }
                if (row["CreatTime"] != null && row["CreatTime"].ToString() != "")
                {
                    entity.CreatTime = DateTime.Parse(row["CreatTime"].ToString());
                }
                if (row["Mark"] != null)
                {
                    entity.Mark = row["Mark"].ToString();
                }
                if (row["field1"] != null)
                {
                    entity.field1 = row["field1"].ToString();
                }
                if (row["field2"] != null)
                {
                    entity.field2 = row["field2"].ToString();
                }
                if (row["field3"] != null)
                {
                    entity.field3 = row["field3"].ToString();
                }
                if (row["field4"] != null)
                {
                    entity.field4 = row["field4"].ToString();
                }
                if (row["field5"] != null)
                {
                    entity.field5 = row["field5"].ToString();
                }
            }
            return entity;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,WarehouseNO,AreaNO,PositionNO,PositionName,Stauts,Designated_Stauts,CapacityUpper,CapacityLower,WeightLimit,CreatTime,Mark,field1,field2,field3,field4,field5 ");
            strSql.Append(" FROM PD_Position ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" NID,WarehouseNO,AreaNO,PositionNO,PositionName,Stauts,Designated_Stauts,CapacityUpper,CapacityLower,WeightLimit,CreatTime,Mark,field1,field2,field3,field4,field5 ");
            strSql.Append(" FROM PD_Position ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<PD_PostionEntity> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,WarehouseNO,AreaNO,PositionNO,PositionName,Stauts,Designated_Stauts,CapacityUpper,CapacityLower,WeightLimit,CreatTime,Mark,field1,field2,field3,field4,field5 ");
            strSql.Append(" FROM PD_Position ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<PD_PostionEntity> list = new List<PD_PostionEntity>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public PD_PostionEntity ReaderBind(IDataReader dataReader)
        {
            PD_PostionEntity entity = new PD_PostionEntity();
            object ojb;
            ojb = dataReader["NID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.NID = (int)ojb;
            }
            entity.WarehouseNO = dataReader["WarehouseNO"].ToString();
            entity.AreaNO = dataReader["AreaNO"].ToString();
            entity.PositionNO = dataReader["PositionNO"].ToString();
            entity.PositionName = dataReader["PositionName"].ToString();
            entity.Stauts = dataReader["Stauts"].ToString();
            entity.Designated_Stauts = dataReader["Designated_Stauts"].ToString();
            ojb = dataReader["CapacityUpper"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.CapacityUpper = (decimal)ojb;
            }
            ojb = dataReader["CapacityLower"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.CapacityLower = (decimal)ojb;
            }
            ojb = dataReader["WeightLimit"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.WeightLimit = (decimal)ojb;
            }
            ojb = dataReader["CreatTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.CreatTime = (DateTime)ojb;
            }
            entity.Mark = dataReader["Mark"].ToString();
            entity.field1 = dataReader["field1"].ToString();
            entity.field2 = dataReader["field2"].ToString();
            entity.field3 = dataReader["field3"].ToString();
            entity.field4 = dataReader["field4"].ToString();
            entity.field5 = dataReader["field5"].ToString();
            return entity;
        }

        #endregion  Method
    }
}
